/*
 * @Title Guava.java
 * @Copyright Copyright(c) 2018
 * @Company com.lizhen
 * @author lizhen
 * @version 0.0.1
 */
package com.lizhen.core.function.guava;

import com.google.common.base.Optional;

/**
 * @author lizhen
 */
public class Guava {

    /**
     * @author lizhen
     */
    public Guava() {
    }

    public static Integer sum(Optional<Integer> a, Optional<Integer> b) {
        // Optional.or - returns the value if present otherwise returns
        // the default value passed.
        Integer value1 = a.or(new Integer(0));
        // Optional.get - gets the value, value should be present
        Integer value2 = b.get();
        return value1 + value2;
    }

    public static void main(String[] args) {
        Guava guava = new Guava();
        guava.optional();
    }

    /**
     * @author lizhen
     */
    private void optional() {
        Guava guava = new Guava();
        Integer value1 = null;
        Integer value2 = new Integer(10);
        // Optional.fromNullable - allows passed parameter to be null.
        Optional<Integer> optional1 = Optional.fromNullable(value1);
        // Optional.of - throws NullPointerException if passed parameter is null
        Optional<Integer> optional2 = Optional.of(value2);

        // Optional.isPresent - checks the value is present or not
        System.out.println("First parameter is present: " + optional1.isPresent());
        System.out.println("Second parameter is present: " + optional2.isPresent());
        System.out.println(sum(optional1, optional2));
    }
}
